#!/usr/bin/env python3
# _*_ coding:utf-8 _*_

from loguru import logger
from urllib.parse import urlparse
import json
import requests


def banner():
    logger.info('''
       _           _          _____               _         _  _   
      / \   _   _ | |_  ___  | ____|__  __ _ __  | |  ___  (_)| |_ 
     / _ \ | | | || __|/ _ \ |  _|  \ \/ /| '_ \ | | / _ \ | || __|
    / ___ \| |_| || |_| (_) || |___  >  < | |_) || || (_) || || |_ 
   /_/   \_\\__,_| \__|\___/ |_____|/_/\_\| .__/ |_| \___/ |_| \__|
                                          |_|                      
            ____                                           
           / ___|__      __ __ _   __ _   __ _   ___  _ __ 
           \___ \\ \ /\ / // _` | / _` | / _` | / _ \| '__|
            ___) |\ V  V /| (_| || (_| || (_| ||  __/| |   
           |____/  \_/\_/  \__,_| \__, | \__, | \___||_|     v1.0 
                                  |___/  |___/             
    
                            by wyzmlr
    ''')


def get_base_path(target_url: str) -> str:
    domain = urlparse(target_url)
    scheme = domain.scheme
    domain = scheme + "://" + domain.netloc
    base_path = ''
    host = ''
    try:
        res = json.loads(requests.get(url=target_url, timeout=5, verify=False).text)
        if "host" in res.keys():
            host = scheme + "://" + res['host']
        else:
            host = domain
        if "basePath" in res.keys():
            base_path = res['basePath']
        elif "servers" in res.keys():
            base_path = res["servers"]['url']
        else:
            base_path = ''
    except Exception as e:
        logger.error("target_url timeout...")
        logger.error(e)
    return (host + base_path).rstrip('/')
